<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Muser extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function loginUser()
    {
        
        $uname = $this->getParamString('username');
        $upass = $this->getParamString('password');
        
        $pass_encode = md5(md5($uname).md5($upass)); //die(''.$pass_encode);
//        $sql = $this->db->query("SELECT * FROM user WHERE username = '".strtolower($uname)."' AND password = '".$pass_encode."' AND active = 1 ");
        $sql = $this->db->query("SELECT * FROM user WHERE username = '".strtolower($uname)."' AND active = 1 ");
        if($sql->num_rows() > 0)
        {
            $data = $sql->row_object();
            if($data->password != $pass_encode) return 201;//sai mật khẩu
//            
//            //Lấy group của id (đã sửa lại database)
//            $id=$data->id;
//            $group=$this->query("SELECT * FROM cpanelwicityver2.manage_group_key  as mg WHERE user_id = '$id'");
//            if($group->num_rows() > 0)$group_id=$group->row_object ()->group_id;
//            
            $arr = array(
                    'id'        => $data->id,
                    'fullname'  => $data->fullname,
                    'username'  => $data->username,
                    'level'     => $data->level,
//                    'manage_group_id' => isset($group_id)?$group_id:null,
                    'manage_group_id' => $data->manage_group_id,  //cái này ko dùng mà lấy luôn từ lúc vào trang
                    'isLogin'   => true
                );
            $this->session->set_userdata($arr);
            return 200;
        }else{
            return 202; //Tài khoản không tồn tại
        }
    }
    
    public function saveUser()
    {
        $uname = $this->getParamString('username');
        $upass = $this->getParamString('password');
        $pass_encode = md5($uname.md5($upass));
        $arr = array(
                'username'    => $uname,
                'password'    => $pass_encode,
                'fullname'    => $this->getParamString('fullname'),
                'mobile'      => $this->getParamString('mobile'),
                'email'       => $this->getParamString('email'),
                'active'      => $this->getParamInt('active'),
                'level'       => 2,
                'created'       => date('Y-m-d H:i:s')
            );
        $this->db->insert('user', $arr);
        $_id = $this->db->insert_id();
        $address = $this->getParamArray('address');
        foreach($address AS $el=>$le)
        {
            $this->db->insert('address_user', array('id_address' => $le, 'id_user' => $_id));
        }
        
        return true;
    }
    
    public function editUser($id)
    {
        $uname = $this->getParamString('username');
        $upass = $this->getParamString('password');
        
        $arr = array(
                'fullname'    => $this->getParamString('fullname'),
                'mobile'      => $this->getParamString('mobile'),
                'email'       => $this->getParamString('email'),
                'active'      => $this->getParamInt('active'),
                'level'       => 2
            );
        
        if($upass != null)
        {
            $pass_encode        = md5($uname.md5($upass));
            $arr['password']    = $pass_encode;
        }    
        
        $this->db->update('user', $arr, "id = ".$id);
        
        $this->db->delete('address_user', array('id_user' => $id));
        $address = $this->getParamArray('address');
        foreach($address AS $el=>$le)
        {
            $this->db->insert('address_user', array('id_address' => $le, 'id_user' => $id));
        }
        
        return true;
    }
    
    public function editUserMem($id)
    {
        $uname = $this->db->query('SELECT username FROM user WHERE id = '.$id)->row_object()->username;
        $upass = $this->getParamString('password');
        
        $arr = array(
                'fullname'    => $this->getParamString('fullname'),
                'mobile'      => $this->getParamString('mobile'),
                'email'       => $this->getParamString('email')
            );
        
        if($upass != null)
        {
            $pass_encode        = md5($uname.md5($upass));
            $arr['password']    = $pass_encode;
        }    
        
        $this->db->update('user', $arr, "id = ".$id);
        return true;
    }
    
    public function deleteUserInfo($id)
    {
        return $this->db->delete('user', "id = ".$id);
    }
    
    public function getUserInfo($id)
    {
        $sql = "SELECT u.* FROM `user` AS u WHERE u.`level` = 2 AND u.id = ".$id;
        
        $xs = $this->db->query($sql)->row_object();
        $id_add = array();
        $_data = $this->db->query("SELECT au.id_address FROM address_user AS au WHERE au.id_user = ".$xs->id)->result_object();
        foreach($_data AS $el=>$le)
        {
            $id_add[] = $le->id_address;
        }
        $xs->add = $id_add;
        return $xs;
    }
    
    public function getUserInfoMem($id)
    {
        $sql = "SELECT u.* FROM `user` AS u WHERE u.id = ".$id;
        
        $xs = $this->db->query($sql)->row_object();
        $id_add = array();
        $_data = $this->db->query("SELECT au.id_address FROM address_user AS au WHERE au.id_user = ".$xs->id)->result_object();
        foreach($_data AS $el=>$le)
        {
            $id_add[] = $le->id_address;
        }
        $xs->add = $id_add;
        return $xs;
    }
    
    public function getUser($number = 0,$offset = 0)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT u.id, u.username, u.fullname, u.mobile FROM `user` AS u WHERE u.`level` = 2 ORDER BY u.id DESC";
        
        $count = $this->db->query($sql)->num_rows();
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        foreach($xs AS $el=>$le)
        {
            $xs[$el]->add = $this->db->query("SELECT CONCAT(ad.address,', ', dt.`name`,', ', pr.`name`) AS `add` FROM address_user AS au INNER JOIN tbl_address AS ad ON au.id_address = ad.id INNER JOIN district AS dt ON ad.id_district = dt.districtid INNER JOIN province AS pr ON ad.id_province = pr.provinceid WHERE au.id_user = ".$le->id)->result_object();
        }
        return array('data' => $xs, 'count' => $count);
    }
    
    public function getPlaceSelect($id = 0)
    {
        $data = $this->db->query('SELECT a.id, p.`name` AS pname, d.`name` AS dname, a.address FROM tbl_address AS a INNER JOIN district AS d ON d.districtid = a.id_district INNER JOIN province AS p ON p.provinceid = a.id_province ORDER BY a.address DESC')->result_array();
        
        $arr = array();
        $id_add = array();
        $where = '';
        if($id != 0)
        {
            $where = "WHERE id_user <> ".$id;
        }
        $_data = $this->db->query("SELECT au.id_address FROM address_user AS au ".$where)->result_object();
        foreach($_data AS $el=>$le)
        {
            $id_add[] = $le->id_address;
        }
        
        foreach($data AS $el=>$le)
        {
            if(!in_array($le['id'], $id_add))
            {
                $arr[$le['id']] = $le['address'].' - '.$le['dname'].' - '.$le['pname'];
            }
        }
        return $arr;
    }
    
	public function getProfile($id){
		return $this->db->query('SELECT username, fullname, mobile, email FROM user WHERE id = '.$id.' ')->row_object();
	}
	
	public function editProfile($id){
		$uname = $this->db->query('SELECT username FROM user WHERE id = '.$id)->row_object()->username;
        $upass = $this->getParamString('password');
        $num_email = $this->db->query('SELECT id FROM user WHERE email = "'.$this->getParamString('email').'" AND id != '.$id.' LIMIT 1 ')->num_rows();
		
		if( $num_email > 0 ){
			return 201;
		}
		
		$arr = array(
                'fullname'    => $this->getParamString('fullname'),
                'mobile'      => $this->getParamString('mobile'),
                'email'       => $this->getParamString('email')
            );
        if($upass != null)
        {
            $pass_encode        = md5(md5($uname).md5($upass));
            $arr['password']    = $pass_encode;
        }  
        $this->db->update('user', $arr, "id = ".$id);
		$this->session->set_userdata(array('fullname' => $this->getParamString('fullname')));
        return 200;
	}
	
}
